package dao;

import pojo.Company;
import utils.JDBCUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CompanyDAOImpl {

    private static final String TABLE_NAME = "Company";

    public static void add(Company company) throws SQLException {
        String sql = "INSERT INTO " + TABLE_NAME + " (company_id, company_name, company_address) VALUES (?, ?, ?)";
        JDBCUtils.execute(sql, null, company.getCompanyName(), company.getCompanyAddress());
    }

    public static void delete(int companyId) throws SQLException {
        String sql = "DELETE FROM " + TABLE_NAME + " WHERE company_id = ?";
        JDBCUtils.execute(sql, companyId);
    }

    public static void update(Company company) throws SQLException {
        String sql = "UPDATE " + TABLE_NAME + " SET company_name = ?, company_address = ? WHERE company_id = ?";
        JDBCUtils.execute(sql, company.getCompanyName(), company.getCompanyAddress(), company.getCompanyID());
    }

    public static Company getById(int companyId) throws SQLException {
        String sql = "SELECT * FROM " + TABLE_NAME + " WHERE company_id = ?";
        ResultSet resultSet = (ResultSet) JDBCUtils.execute(sql, companyId);
        if (resultSet.next()) {
            return createCompanyFromResultSet(resultSet);
        }
        return null;
    }

    public static List<Company> getAll() throws SQLException {
        List<Company> companies = new ArrayList<>();
        String sql = "SELECT * FROM " + TABLE_NAME;
        ResultSet resultSet = (ResultSet) JDBCUtils.execute(sql);
        while (resultSet.next()) {
            Company company = createCompanyFromResultSet(resultSet);
            companies.add(company);
        }
        return companies;
    }

    public static int getCount() throws SQLException {
        String sql = "SELECT COUNT(*) AS count FROM " + TABLE_NAME;
        ResultSet resultSet = (ResultSet) JDBCUtils.execute(sql);
        if (resultSet.next()) {
            return resultSet.getInt("count");
        }
        return 0;
    }

    public static List<Company> getByPage(int start, int pageSize) throws SQLException {
        List<Company> companies = new ArrayList<>();
        String sql = "SELECT * FROM " + TABLE_NAME + " LIMIT ?, ?";
        ResultSet resultSet = (ResultSet) JDBCUtils.execute(sql, start, pageSize);
        while (resultSet.next()) {
            Company company = createCompanyFromResultSet(resultSet);
            companies.add(company);
        }
        return companies;
    }

    public static List<Company> findByKeyword(String keyword) throws SQLException {
        List<Company> companies = new ArrayList<>();
        String sql = "SELECT * FROM " + TABLE_NAME + " WHERE company_name LIKE ?";
        ResultSet resultSet = (ResultSet) JDBCUtils.execute(sql, "%" + keyword + "%");
        while (resultSet.next()) {
            Company company = createCompanyFromResultSet(resultSet);
            companies.add(company);
        }
        return companies;
    }

    public static List<Company> findByPageAndKeyword(int start, int pageSize, String keyword) throws SQLException {
        List<Company> companies = new ArrayList<>();
        String sql = "SELECT * FROM " + TABLE_NAME + " WHERE company_name LIKE ? LIMIT ?, ?";
        ResultSet resultSet = (ResultSet) JDBCUtils.execute(sql, "%" + keyword + "%", start, pageSize);
        while (resultSet.next()) {
            Company company = createCompanyFromResultSet(resultSet);
            companies.add(company);
        }
        return companies;
    }

    private static Company createCompanyFromResultSet(ResultSet resultSet) throws SQLException {
        int companyId = resultSet.getInt("company_id");
        String companyName = resultSet.getString("company_name");
        String companyAddress = resultSet.getString("company_address");
        return new Company(companyId, companyName, companyAddress);
    }
}
